package smb;

import java.io.*;
import java.net.*;

public class User {
	private String _username;
	private String _serverName;
	private int _port;
	
	
	public User(String username, String serverName,int port){
		_username = username;
		_serverName = serverName;
		_port = port;		
	}
	
	public void run() throws IOException {
		
		Socket socket = null;
		PrintWriter out = null;
		BufferedReader in = null;
		
		try {
			
			try {
				socket = new Socket(_serverName,_port);
				out = new PrintWriter(socket.getOutputStream(),true);
				in = new BufferedReader(new InputStreamReader( socket.getInputStream()));
			} catch (UnknownHostException e) {
				System.err.println("Don't know about host:" + _serverName);
				throw e;
			} catch (IOException e) {
				System.err.println("Couldn't get I/O for " + "the connection to: " + _serverName + ":" + _port);
				throw e;
			}
			
			String request = "REQ " + _username;
			String serverInput = null;
			
			out.println(request);
			
			try {
				serverInput = in.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				System.err.println("Error receiving message through connection established");			
				throw e;
				
			}
			
			System.out.println(serverInput.substring(3));	
			
		} finally {
			if(socket != null)out.close();
			if(in != null)in.close();
			if(socket != null)socket.close();
		}	

	}
	
	
	/*
	 * Command line arguments:
	 * "User username [-n SMBname] [-p SMBport]"
	 */
	public static void main(String[] args) throws IOException{		
		String userName;
		String SMBname = "localhost";
		int SMBport = 58043;
		
		if( args.length == 0){
			System.err.println("Error: Must enter username");
			System.err.println("Usage: User username [-n SMBname] [-p SMBport]");
			System.exit(-1);
		}
		
		userName = args[0];
		

		if( args.length == 3){
			/*
			 * Only one parameter given
			 */
			if( args[1].equals("-n") ){
				SMBname = args[2];
			}else if( args[1].equals("-p") ){
				try {
					SMBport = Integer.parseInt(args[2]);
				} catch (NumberFormatException e) {
					System.err.println("Command line \"-p\" argument must be an integer");
					System.exit(-1);
				}				
			}else{
				System.err.println("Command line parameter unknown");
				
			}
		}else if( args.length == 5){
			/*
			 * Two parameters were given
			 */
			if(args[1] != "-n" && args[3] != "-p"){
				System.err.println("Invalid parameters");
				System.exit(-1);
			}else{
				SMBname = args[2];
				try {
					SMBport = Integer.parseInt(args[4]);
				} catch (NumberFormatException e) {
					System.err.println("Command line \"-p\" argument must be an integer");
					System.exit(-1);
				}			
			}			
		}else if( args.length != 1){
			System.err.println("Invalid arguments");
			System.err.println("Usage: User username [-n SMBname] [-p SMBport]");
			System.exit(-1);
		}
		
		User user = new User(userName,SMBname,SMBport);
		
		user.run();

	}
}
